package com.pdstudio.carrecom.ui.activity;

import net.simonvt.menudrawer.MenuDrawer;
import net.simonvt.menudrawer.MenuDrawer.OnDrawerStateChangeListener;
import net.simonvt.menudrawer.Position;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import com.androidquery.AQuery;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.common.BaseFragmentActivity;
import com.pdstudio.carrecom.ui.activity.more.ActivityMoreMain;
import com.pdstudio.carrecom.ui.activity.order.ActivityOrderMain;
import com.pdstudio.carrecom.ui.fragment.FragmentSelectBrands;
import com.pdstudio.carrecom.ui.view.SlideMenuView;

/**
 * 品牌选择，这里可以使用品牌选择fragment跟划出菜单来实现
 * 类: ActivitySelectBrandsSlide <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-5-18 上午11:19:17
 */
public class ActivitySelectBrandsSlide extends BaseFragmentActivity {

	private static final String STATE_ACTIVE_POSITION = "net.simonvt.menudrawer.samples.ContentSample.activePosition";
	private static final String STATE_CONTENT_TEXT = "net.simonvt.menudrawer.samples.ContentSample.contentText";

	AQuery mAquery;

	private int mActivePosition = -1;
	private String mContentText;

	//Column mWaitColumn; // 等待打开功能的栏目,用于需要校验用户的功能

	Fragment mNowFragment; // 现在正在打开的fragment

	//Column mSelectColumn; // 选中栏目

	SlideMenuView mSlideMenu; // 侧边菜单

	int mAction = 0; // 等待的动作(需要用户先登录)

	final static int ACTION_CIRCLE = 1;
	final static int ACTION_USER = 2;

	String mUserName;

	public MenuDrawer mMenuDrawer; // 侧边框
	Context _context;
	boolean _isCheckLogin = false;
	boolean _isFirstFragment = true; // 是否显示在热点动态
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		// super在前面 mTitlebar才有效
		super.onCreate(arg0);
		_context = this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		if (arg0 != null) {
			mActivePosition = arg0.getInt(STATE_ACTIVE_POSITION);
			mContentText = arg0.getString(STATE_CONTENT_TEXT);
		}

		setMenu();

		mAquery = new AQuery(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(STATE_ACTIVE_POSITION, mActivePosition);
		outState.putString(STATE_CONTENT_TEXT, mContentText);
	}

	// 设置侧边框.主界面也在此设置
	private void setMenu() {

		mMenuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_CONTENT,
				Position.RIGHT);
		mMenuDrawer.setContentView(R.layout.activity_select_brands_slide);
		mMenuDrawer.setTouchMode(MenuDrawer.TOUCH_MODE_FULLSCREEN);

		mSlideMenu = new SlideMenuView(ActivitySelectBrandsSlide.this);
		mSlideMenu.setData(_listData, _listIcon);
		mSlideMenu.setOnItemClicked(_listerItemListener);

		// int width = mSlideMenu.getMeasuredWidth();
		// if (width > 0) {
		// mMenuDrawer.setMenuSize(width);
		// }

		DisplayMetrics dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);

		mMenuDrawer.setMenuSize(dm.widthPixels / 2);

		mMenuDrawer.setMenuView(mSlideMenu);
		mMenuDrawer.toggleMenu();

		beginFirstFragment();

		// 初始化界面
		// changeFragmentNewsList(1);
		new Handler().postDelayed(new Runnable() {
			public void run() {
				mMenuDrawer.closeMenu();
			}
		}, 2500);

	}

	String[] _listData = new String[] { "新闻资讯",
			"站点动态", // "新闻动态",
			"在线学习", "电子期刊", "博客广场", "微博广场", "手机报", "相册集", "群工站", "我的好友",
			"我的组织", "民意调查", "党员呼声", "三会一课", "系统设置" };

	Integer[] _listIcon = new Integer[] {
			R.drawable.menu_blog,
			R.drawable.menu_blog, // R.drawable.menu_news,
			R.drawable.menu_blog, R.drawable.menu_blog,
			R.drawable.menu_blog, R.drawable.menu_blog, R.drawable.menu_blog,
			R.drawable.menu_blog, R.drawable.menu_blog,
			R.drawable.menu_blog, R.drawable.menu_blog, R.drawable.menu_blog,
			R.drawable.menu_blog, R.drawable.menu_blog,
			R.drawable.menu_blog };

	OnItemClickListener _listerItemListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			_isCheckLogin = false;
			mNowFragment = null;
			_isFirstFragment = false;

			mMenuDrawer.setTouchMode(MenuDrawer.TOUCH_MODE_FULLSCREEN);

			switch (arg2) {
			case 0:
				finish();
				break;
			case 1:
				Intent intent=new Intent(ActivitySelectBrandsSlide.this,ActivityOrderMain.class);
				startActivity(intent);
				break;
			case 2:
				Intent intent1=new Intent(ActivitySelectBrandsSlide.this,ActivityMoreMain.class);
				startActivity(intent1);
				break;
			case 4:
				finish();
				break;
			case 5:
				finish();
				break;
			case 9:
				// 我的好友
				break;
			case 7:
				// mAction = ACTION_USER;
				// 相册广场
				break;
			case 13:
				// 三会一课
				break;
			case 10:
				// 我的组织
				break;
			case 11:
				// "民意调查"
				break;

			case 12:
				// "党员呼声"
				break;
			case 6:
				// 手机报
				break;
			case 8:
				// /群工站
				
				break;
			case 3:
				Intent intent2=new Intent(ActivitySelectBrandsSlide.this,ActivityCall.class);
				startActivity(intent2);
				break;
			case 14:
				break;
			}

			if (mNowFragment != null) {

				openFragment(mNowFragment);

				// new Handler().postDelayed(new Runnable() {
				// public void run() {
				// getSupportFragmentManager().beginTransaction()
				// .replace(R.id.fg_fragment, mNowFragment)
				// .commit(); // 允许丢失状态
				// }
				// }, 400);
			}

		}
	};

	public void openFragment(final Fragment fragment) {
		mMenuDrawer.closeMenu();
		mMenuDrawer
				.setOnDrawerStateChangeListener(new OnDrawerStateChangeListener() {
					@Override
					public void onDrawerStateChange(int oldState, int newState) {
						// TODO Auto-generated method stub
						if (newState == MenuDrawer.STATE_CLOSED
								&& mNowFragment != null) {
							getSupportFragmentManager().beginTransaction()
									.replace(R.id.fg_fragment, fragment)
									.commit(); // 允许丢失状态

							mNowFragment = null;

						}

						if (newState == MenuDrawer.STATE_OPEN) {
							

						}
					}
				});

	}

	public void openMenu() {
		if (mMenuDrawer != null)
			mMenuDrawer.toggleMenu();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			// 在这里做你想做的事情
			openMenu();
			// super.openOptionsMenu(); // 调用这个，就可以弹出菜单
			return true;
		}

		return super.onKeyDown(keyCode, event);
		// 最后，一定要做完以后返回 true，或者在弹出菜单后返回true，其他键返回super，让其他键默认
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
			getSupportFragmentManager().popBackStack();
			return;
		}

		final int drawerState = mMenuDrawer.getDrawerState();
		if (drawerState == MenuDrawer.STATE_OPEN
				|| drawerState == MenuDrawer.STATE_OPENING) {
			mMenuDrawer.closeMenu();
			return;
		} else {

			if (_isFirstFragment) {
				//UIHelper.exitSystem(ActivitySelectBrandsSlide.this);
				finish();
			} else {
				beginFirstFragment();
			}
			// mMenuDrawer.openMenu();
			return;

		}

	}

	private void beginFirstFragment() {
		_isFirstFragment = true;
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.fg_fragment, new FragmentSelectBrands()).commit(); // 允许丢失状态
	}

}
